<?php
use Core\Config;
use Core\Mvc\Application;
use Core\Security;
use Core\Cache;
use Core\Forms\Init as FormsInit;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Db\Adapter\MongoDB\Client as MongoClient;

define('WEB_CODE', $site[$_SERVER['HTTP_HOST']]);
define('ROOT_DIR', __DIR__ . '/../../');
define('MODULES_DIR', ROOT_DIR . 'Modules/');
define('THEMES_DIR', ROOT_DIR . 'Themes/');
define('WEB_DIR', ROOT_DIR . WEB_CODE . '/');
define('CACHE_DIR', ROOT_DIR . 'Web/' . WEB_CODE . '/cache/');
define('CONFIG_DIR', ROOT_DIR . 'Web/' . WEB_CODE . '/config/');
define('DOWNLOAD_DIR', ROOT_DIR . 'Web/' . WEB_CODE . '/cache/download/');
// 初始化设置组件
$configObject = new Config();
$config = $configObject->get('config');
define('ADMIN_PREFIX', $config['adminPrefix']);
define('DEBUG', $config['debug']);
// 是否在开发者模式下运行

ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);

date_default_timezone_set($config['timezone']);
$di = new FactoryDefault();
$di->setShared('config',$configObject);
$di->setShared('translate', 'Core\Translate');
$di->setShared('assets', 'Core\Assets\Manager');
$di->setShared('filter', 'Core\Filter');
$di->setShared('collectionManager', 'Phalcon\Mvc\Collection\Manager');
$di->setShared('volt', 'Core\Mvc\View\Volt');
$di->setShared('view', 'Core\Mvc\View');
$di->setShared('entityManager', 'Core\Mvc\EntityManager');
$di->setShared('router', 'Core\Mvc\Router');
$di->setShared('user', 'Core\Security\User');
$di->setShared('form', 'Core\Forms\Manager');
$di->setShared('filter', 'Core\Filter');

// 注册session服务
$di->setShared('session', function () use ($config) {
    if (!isset($config['session'])) {
        $config['session'] = [
            'adapter' => 'Files',
            'config' => []
        ];
    }
    $sessionAdapter = '\Phalcon\Session\Adapter\\' . $config['session']['adapter'];
    $session = new $sessionAdapter($config['session']['config']);
    $session->start();
    return $session;
});
// 注册数据库服务
$di->setShared('mongo', function () use ($config) {
    if (!isset($config['mongo']['username']) || !isset($config['mongo']['password'])) {
        $dsn = 'mongodb://' . $config['mongo']['host'];
    } else {
        $dsn = sprintf(
            'mongodb://%s:%s@%s',
            $config['mongo']['username'],
            $config['mongo']['password'],
            $config['mongo']['host']
        );
    }

    $mongo = new MongoClient($dsn);

    return $mongo->selectDatabase($config['mongo']['dbname']);
});
// 注入模型缓存服务
$di->setShared('modelsCache', function () use ($config) {
    $cache = new Cache();
    return $cache->getCache($config['modelsCache'], 'data');
});
// 注入视图缓存服务
$di->setShared('viewCache', function () use ($config) {
    $cache = new Cache();
    return $cache->getCache($config['viewCache'], 'output');
});
// 注册消息提醒服务
$di->setShared('flash', [
    'className' => 'Phalcon\Flash\Direct',
    'arguments' => [
        [
            'type' => 'parameter',
            'value' => $config['flash']
        ]
    ]
]);
// 註冊消息提醒服務
$di->setShared('flashSession', [
    'className' => 'Phalcon\Flash\Session',
    'arguments' => [
        [
            'type' => 'parameter',
            'value' => $config['flash']
        ]
    ]
]);
// 注册加密组件
$di->setShared('crypt', [
    'className' => 'Phalcon\Crypt',
    'calls' => [
        [
            'method' => 'setKey',
            'arguments' => [
                [
                    'type' => 'parameter',
                    'value' => $config['cryptEncode']
                ]
            ]
        ]
    ]
]);
FormsInit::init();
// 加载模块
$modulesList = $configObject->get('modules');
foreach ($modulesList as $value) {
    require __DIR__ . '/../../Modules/' . ucfirst($value) . '/Module.php';
}
// 注册调度器服务
$security = new Security();
$di->setShared('security', $security);
$di->getShared('eventsManager')->attach('dispatch', $security);
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($di->getShared('eventsManager'));
$di->setShared('dispatcher', $dispatcher);
try {
    // 创建应用
    $application = new Application($di);
    echo $application->handle()->getContent();
} catch (\Exception $e) {
    echo $e->getMessage().PHP_EOL;
    echo '文件'.$e->getFile().PHP_EOL;
    echo '行：'.$e->getLine().PHP_EOL;
}
